<template>
    <div class="login">
        <div class="loginBox">
            <section>
                <img src="@/assets/logo.png" alt="">
            </section>
            <el-form ref="formLogin" :model="form" :rules="LoginRules">
                <el-form-item prop="username">
                    <el-input v-model="form.username" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" prefix-icon="el-icon-s-goods"></el-input>
                </el-form-item>
                <el-form-item class="foot">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>


<script>
import { postLogin } from '@/utils/axios/loginApi'
import { mapMutations } from 'vuex';
export default {
    data() {
        return {
            form: {
                username: '',
                password: ''
            },
            LoginRules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        ...mapMutations(['addToken']),
        login() {
            this.$refs.formLogin.validate(async (valid) => {
                if (valid) {
                    let { data: res } = await postLogin(this.form)
                    if (res.meta.status == 200) {
                        this.$message.success(res.meta.msg)
                        this.addToken(res.data)
                        this.$router.push('/')
                    } else {
                        this.$message.error(res.meta.msg)
                    }
                }
            });
        },
        resetForm() {
            this.$refs.formLogin.resetFields();
        }
    }
}
</script>


<style lang="scss">
.login {
    width: 100%;
    height: 100%;
    background: #2b4b6b;
    display: flex;
    justify-content: center;
    align-items: center;
}

.loginBox {
    width: 450px;
    height: 300px;
    background: white;

    section {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        background: white;
        position: relative;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 999;
        box-shadow: 0 0 10px #eee;
        display: flex;
        justify-content: center;
        align-items: center;

        img {
            width: 80%;
            height: 80%;
            box-sizing: border-box;
            border-radius: 50%;

        }
    }

    .el-form {
        width: 90%;
        margin: auto;
    }
}

.foot {
    display: flex;
    justify-content: flex-end;
}
</style>